package edu.kpi.pzks2.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

import edu.kpi.pzks2.graph.Edge;
import edu.kpi.pzks2.graph.HasWeight;
import edu.kpi.pzks2.graph.Node;
import edu.uci.ics.jung.visualization.VisualizationViewer;

@SuppressWarnings("serial")
public class WeightDialog extends JDialog {
    private final JPanel contentPanel = new JPanel();
    private JTextField textField;

    /**
     * Create the dialog.
     */
    public WeightDialog(final HasWeight hasWeight, final VisualizationViewer<Node, Edge> visual) {
        setSize(250, 100);
        setTitle("Params");
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new MigLayout("", "[][grow]", "[]"));

        JLabel lblWeight = new JLabel("Weight");
        contentPanel.add(lblWeight, "cell 0 0,alignx trailing");
        textField = new JTextField();
        contentPanel.add(textField, "cell 1 0,growx");
        textField.setColumns(10);
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        JButton okButton = new JButton("OK");
        
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                hasWeight.setWeight(Integer.valueOf(textField.getText()));
                visual.repaint();
                WeightDialog.this.setVisible(false);
            }
        });
        
        okButton.setActionCommand("OK");
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);

        JButton cancelButton = new JButton("Cancel");
        
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                WeightDialog.this.setVisible(false);
            }
        });
        
        cancelButton.setActionCommand("Cancel");
        buttonPane.add(cancelButton);
    }

}
